package com.jgybzx.my_time_at_portia.entity;

import com.jgybzx.my_time_at_portia.entity.base.Item;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 郭江勇
 * @ClassName CraftingSystem
 * @description 合成系统类
 * @date 2025/02/19 17:10
 */
public class CraftingSystem {
    private Map<Integer, Item> itemDatabase = new HashMap<>();
    private Map<Integer, CraftingRecipe> recipeDatabase = new HashMap<>();

    // 递归计算合成物品所需的材料
    public Map<Item, Integer> getRequiredMaterials(Item product, int quantity) {
        Map<Item, Integer> requiredMaterials = new HashMap<>();
        calculateMaterials(product, quantity, requiredMaterials);
        return requiredMaterials;
    }

    private void calculateMaterials(Item item, int quantity, Map<Item, Integer> requiredMaterials) {
        // 获取合成配方
        CraftingRecipe recipe = recipeDatabase.get(item.getItemId());
        if (recipe == null) {
            // 如果该物品没有合成配方，说明是原材料，直接添加
            requiredMaterials.put(item, requiredMaterials.getOrDefault(item, 0) + quantity);
            return;
        }

        // 遍历配方中的原材料
        for (CraftingRecipe.Ingredient ingredient : recipe.getIngredients()) {
            int requiredQuantity = ingredient.getQuantity() * quantity;
            calculateMaterials(ingredient.getItem(), requiredQuantity, requiredMaterials);
        }
    }
}
